package com.fjut.kms.dao;

import com.fjut.kms.domain.User;
import com.fjut.kms.exception.DataAccessException;
import com.fjut.kms.tools.DBTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Empathy on 2017/7/12.
 */
public class UserDaoJDBCImpl implements UserDao {

    private static final String SQL_GETUSER_BYNAME="select password,status from users where username = ?";
    private static final String SQL_UPDATE_USER_LOGINTIMES="update users set loginTimes=loginTimes+1 where username=?";
    private static final String SQL_INSERT_LOGINRECORD="insert into loginRecord values(null,?,?,?,?,?)";
    private static final String SQL_GETUSERNAME_BUUSERNAME="select username from users where username=?";
    private static final String SQL_ADD_USER="insert into users values(?,?,?,?,?,?,?,0,0,0,?,null)";
    private static final String SQL_UPDATE_PASS="update users set password=? where username=?";
    private static final String SQL_GET_DEPT="select deptname from users where username=?";

    public User getUserByName(String userName){

        Connection conn= DBTools.getConn();
        PreparedStatement pstmt=null;
        ResultSet rset =null;
        User user =null;
        try{
            pstmt = conn.prepareStatement(SQL_GETUSER_BYNAME);
            pstmt.setString(1,userName);
            rset=pstmt.executeQuery();

            if(rset.next()){
                user =new User();
                user.setUserName(userName);
                user.setUserPass(rset.getString("password"));
                user.setUserStatus(rset.getString("status"));
            }
            else
                throw  new DataAccessException("用户名为["+userName+"[的用户不存在，请检查用户名！");

        }catch (SQLException e){
            e.printStackTrace();
        }
        return user;
    }

    public void updateLoginTimes(String userName){
        Connection conn= DBTools.getConn();
        PreparedStatement pstmt=null;
        try{
            pstmt=conn.prepareStatement(SQL_UPDATE_USER_LOGINTIMES);
            pstmt.setString(1,userName);
            pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBTools.releaseRes(conn,pstmt,null);
        }
    }

    public void addLoginRecord(User user){
        Connection conn= DBTools.getConn();
        PreparedStatement pstmt=null;
        try{
            pstmt=conn.prepareStatement(SQL_INSERT_LOGINRECORD);
            pstmt.setString(1,user.getUserName());
            pstmt.setString(2,user.getMachineId());
            pstmt.setString(3,user.getMac());
            pstmt.setString(4,user.getIp());
            pstmt.setString(5,user.getLoginDate());
            pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBTools.releaseRes(conn,pstmt,null);
        }
    }

    public String getUserNameByUserName(String userName){
        Connection conn= DBTools.getConn();
        PreparedStatement pstmt=null;
        ResultSet rset =null;
        String result=null;
        try{
            pstmt = conn.prepareStatement(SQL_GETUSERNAME_BUUSERNAME);
            pstmt.setString(1,userName);
            rset=pstmt.executeQuery();
            if (rset.next()){
                result=rset.getString("username");
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBTools.releaseRes(conn,pstmt,null);
        }
        return result;
    }
public  String getDept(String userName)
{
	
	 Connection conn= DBTools.getConn();
	 PreparedStatement pstmt=null;
	 ResultSet rset =null;
	 String result=null;
	 try{
		 pstmt = conn.prepareStatement(SQL_GET_DEPT);
		 pstmt.setString(1,userName);
		 rset=pstmt.executeQuery();
		 if (rset.next()){
			 result=rset.getString("deptname");
		 }
	 }catch (SQLException e){
		 e.printStackTrace();
	 }finally {
		 DBTools.releaseRes(conn,pstmt,null);
}
return result;

}
    public void addUser(User user){
        Connection conn= DBTools.getConn();
        PreparedStatement pstmt=null;
        try{
            pstmt=conn.prepareStatement(SQL_ADD_USER);
            pstmt.setString(1,user.getUserName());
            pstmt.setString(2,user.getUserPass());
            pstmt.setString(3,user.getCnName());
            pstmt.setString(4,user.getCategory());
            pstmt.setString(5,user.getDepyName());
            pstmt.setString(6,user.getEmail());
            pstmt.setString(7,user.getUserStatus());
            pstmt.setString(8,user.getRegTime());
            pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBTools.releaseRes(conn,pstmt,null);
        }
    }

    public void updatePassByUserName(String userName,String pass){
        Connection conn= DBTools.getConn();
        PreparedStatement pstmt=null;
        try{
            pstmt=conn.prepareStatement(SQL_UPDATE_PASS);
            pstmt.setString(1,pass);
            pstmt.setString(2,userName);
            pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBTools.releaseRes(conn,pstmt,null);
        }
    }


}
